package com.zhht.checkcop.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;
import com.zhht.checkcop.parser.ParserMovie;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 持续更新最新电影用.   只需要调用本类和 MovieInfoInitialization 类
 * @author love
 *
 */
public class MovieUpdate implements PageProcessor{
	private final static Logger log= Logger.getLogger(MovieUpdate.class);
	private Boolean pageURL=new Boolean(true);
	
	private Site site = Site.me().setCycleRetryTimes(5).setSleepTime(1500).setTimeOut(6000);
	private String classifyId;
	private String typeName;
	private String type;
	public MovieUpdate(){}
	public MovieUpdate(String typeName,String classifyId,String type){
		this.typeName = typeName;
		this.classifyId=classifyId;
		this.type = type;
	}

	public void process(Page page) {
    	try{
	    	List<String> frontCoverUrl=page.getHtml().xpath("//div[@class='pic']/a/img/@src").all();	//封面的图片URL
	    	List<String> infoUrl=page.getHtml().xpath("//div[@class='txt']/h3/a/@href").all();		//详情URL
	    	
	    	List<String> movieName = page.getHtml().xpath("//div[@class='txt']/h3/a/allText()").all();
	    	
//	    	List<String> movieName=page.getHtml().xpath("//div[@class='txt']/h3/a/b/font/text()").all();	//电影名称
	    	List<String> updateTime=page.getHtml().xpath("//div[@class='txt']/span/text()").all();//是否推荐
	    	if(null != frontCoverUrl && frontCoverUrl.size()>0){
		    	page.putField("frontCoverUrl", frontCoverUrl);
		    	page.putField("infoUrl", infoUrl);
		    	page.putField("movieName", movieName);
		    	page.putField("updateTime", updateTime);
		    	page.putField("classifyId", classifyId);
		    	page.putField("infoClassifyName", typeName);
		    	
		    	String currentPage = page.getHtml().xpath("//li[@class='total']/span/text()").get();
		    	log.info("当前类型:"+typeName+"------------------------------currentPage:"+currentPage);
		    	if(pageURL){
		    		String pageNum = page.getHtml().xpath("//li[@class='total']/span/text()").get();
		    		System.out.println(pageNum);
		    		if(pageNum!=null&&pageNum.length()>2){
		    			Integer dbCount=0;
		    			Integer newCount=0;
		    			newCount = Integer.parseInt(pageNum.substring(pageNum.indexOf("页")+1, pageNum.length()-1));
		    			try{
		    	        	MysqlDb mysqldb = new MysqlDb();
		    	        	mysqldb.connect();
		    	        	List<Map<String, Object>> str = mysqldb.query("select count(1) count from movie_info where classifyId = "+classifyId);
		    	        	dbCount = Integer.parseInt(str.get(0).get("count")+"");
			    			mysqldb.close();
		    	    	}catch (Exception e) {
		    				e.printStackTrace();
		    			}
		    			
		    			Integer sousuoPageNum = (newCount-dbCount)/10+2;
		    			
		    			page.addTargetRequests(getPageNum(sousuoPageNum,type));
		    		}
		    		pageURL=false;
		    	}
	    	}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    public List<String> getPageNum(Integer pageNum,String type){
    	List<String> page=new ArrayList<String>();
    	for(int i=2;i<=pageNum;i++){
    		page.add("https://www.piaohua.com/html/"+type+"/list_"+i+".html");
    	}
    	return page;
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
    	try{
        	MysqlDb mysqldb = new MysqlDb();
        	mysqldb.connect();
        	StaticDBMap.existNovelNameMap = mysqldb.query("select id,infoUrl from movie_info where d = 0 and and classifyId != 10",new ArrayList(),"infoUrl");
        	mysqldb.close();
    	}catch (Exception e) {
			e.printStackTrace();
		}

    	int thread=args!=null&&args.length>0?Integer.parseInt(args[0]):5;
	    StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("动作片","1","dongzuo")).addUrl("https://www.piaohua.com/html/dongzuo/index.html").addPipeline(new ParserMovie("dongzuo","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("动作片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("喜剧片","2","xiju")).addUrl("https://www.piaohua.com/html/xiju/index.html").addPipeline(new ParserMovie("xiju","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("喜剧片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("爱情片","3","aiqing")).addUrl("https://www.piaohua.com/html/aiqing/index.html").addPipeline(new ParserMovie("aiqing","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("爱情片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("科幻片","4","kehuan")).addUrl("https://www.piaohua.com/html/kehuan/index.html").addPipeline(new ParserMovie("kehuan","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("科幻片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("剧情片","5","juqing")).addUrl("https://www.piaohua.com/html/juqing/index.html").addPipeline(new ParserMovie("juqing","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("剧情片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("悬疑片","6","xuannian")).addUrl("https://www.piaohua.com/html/xuannian/index.html").addPipeline(new ParserMovie("xuannian","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("悬疑片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("战争片","7","zhanzheng")).addUrl("https://www.piaohua.com/html/zhanzheng/index.html").addPipeline(new ParserMovie("zhanzheng","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("战争片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("恐怖片","8","kongbu")).addUrl("https://www.piaohua.com/html/kongbu/index.html").addPipeline(new ParserMovie("kongbu","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("恐怖片已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("灾难片","9","zainan")).addUrl("https://www.piaohua.com/html/zainan/index.html").addPipeline(new ParserMovie("zainan","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("灾难片已完成");
//        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
//        Spider.create(new MovieUpdate("连续剧","10","lianxuju")).addUrl("https://www.piaohua.com/html/lianxuju/index.html").addPipeline(new ParserMovie("lianxuju","https://www.piaohua.com")).thread(thread).run();
//        saveAndUpdate();
//        log.info("连续剧已完成");
        StaticDBMap.writeNovelNameMap=new HashMap<String, Map<String,Object>>();
        Spider.create(new MovieUpdate("动漫","11","dongman")).addUrl("https://www.piaohua.com/html/dongman/index.html").addPipeline(new ParserMovie("dongman","https://www.piaohua.com")).thread(thread).run();
        saveAndUpdate();
        log.info("动漫已完成");
    }
    
    
	private static void saveAndUpdate() {
		try{
        	MysqlDb mysqldb = new MysqlDb();
        	mysqldb.connect();
        	if(null != StaticDBMap.writeNovelNameMap && StaticDBMap.writeNovelNameMap.size() > 0){
	        	//新增
	        	Iterator<Entry<String, Map<String,Object>>> iter = StaticDBMap.writeNovelNameMap.entrySet().iterator();
	        	List<String[]> sqls=new ArrayList<String[]>();
	        	while (iter.hasNext()) {
	        		Map.Entry<String, Map<String,Object>> entry = iter.next();
	        		sqls.add(mysqldb.assemblyInsertSqls("movie_info",entry.getValue()));
	            }
	        	mysqldb.executeBatchs(sqls);
        	}
        	mysqldb.close();
        }catch (Exception e) {
        	e.printStackTrace();
        }
	}
}
